package com.gitee.fsmxm.zd.bili;

import com.gitee.fsmxm.zd.bili.listener.MyWebSocketListener;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.nio.ByteBuffer;

/**
 * status code
 * https://datatracker.ietf.org/doc/html/rfc6455#section-7.4.1
 * https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
 */
public class DanmuWebSocketClient extends WebSocketClient {

    public DanmuWebSocketClient(URI serverUri) {
        super(serverUri);
    }

    private MyWebSocketListener listener;

    public void setListener(MyWebSocketListener listener) {
        this.listener = listener;
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        if (listener != null) {
            listener.onOpen(serverHandshake);
        }
    }

    @Override
    public void onMessage(String s) {
        if (listener != null) {
            listener.onMessage(s);
        }
    }

    @Override
    public void onMessage(ByteBuffer bytes) {
        if (listener != null) {
            listener.onMessage(bytes);
        }
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        if (listener != null) {
            listener.onClose(code, reason, remote);
        }
    }

    @Override
    public void onError(Exception e) {
        if (listener != null) {
            listener.onError(e);
        }
    }
}
